package com.sc.datacollect.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;

public class UnifiedcodeUtil {

	@SuppressWarnings("deprecation")
	public static List<UnifiedCodePo> convert(String url, List<UnifiedCodePo> pos){
		ParserConfig pc = new ParserConfig(UnifiedCodePo.class.getClassLoader());
		JSONArray listArray = (JSONArray) JSON.toJSON(pos, pc);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("data", listArray.toJSONString());
		
		Client c = Client.getInstance();
		String result = c.execute(url, params, null, RequestMethod.POST);
		
		return JSON.parseArray(result, UnifiedCodePo.class);
	}
	
	@SuppressWarnings("deprecation")
	public static List<UnifiedCodePo> convert(List<UnifiedCodePo> pos){
		ParserConfig pc = new ParserConfig(UnifiedCodePo.class.getClassLoader());
		JSONArray listArray = (JSONArray) JSON.toJSON(pos, pc);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("data", listArray.toJSONString());
		
		Client c = Client.getInstance();
		String result = c.execute("unifiedcode-getCodeInfo-list", params, null, RequestMethod.POST);
		
		JSONObject obj = (JSONObject) JSONObject.parse(result);
		
		return JSON.parseArray(obj.getString("data"), UnifiedCodePo.class);
	}
	
	public static String getMyCode(List<UnifiedCodePo> pos, String inCode) {
		String myCode = inCode;
		
		for (UnifiedCodePo unifiedCodePo : pos) {
			if (unifiedCodePo.getInCode().equals(inCode)) {
				myCode = unifiedCodePo.getMyCode();
				
				break;
			}
		}
		
		return myCode;
	}

}
